package com.flower8.controller;

import com.flower8.service.SysLogService;
import com.flower8.service.TokenManager;
import com.flower8.sysconfig.constants.MsgConstants;
import com.flower8.utils.PageData;
import com.flower8.utils.SpringUtil;

import org.apache.shiro.SecurityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * @Description: 公共的Controller
 * @Author: tanglin
 * @Date: 2019/08/29 10:45
 * @Version: 1.0
 */
public class BaseController {
	private static final Logger logger = LoggerFactory.getLogger(BaseController.class);
	protected SysLogService logService;
	/**
	 * 请求接口返回状态码
	 */
	public static final int FAILURE = 0;// 失败
	public static final int SUCCESS = 1;// 成功

	/**
	 * 用于计算接口的响应时间（多线程时除外）
	 */
	private static long startTime = 0;
	private static long endTime = 0;

	/**
	 * 返回数据（错误）
	 * 
	 * @param message
	 */
	protected Object responseResultForError(String message) {
		return responseResultForErrorField(null, message);
	}

	/**
	 * 返回数据（错误字段）
	 * 
	 * @param field
	 * @param message
	 */
	protected Object responseResultForErrorField(String field, String message) {
		return responseResult(null, field, true, message);
	}

	/**
	 * 返回数据（成功）
	 * 
	 * @param data
	 * @param message
	 * @return
	 */
	protected Object responseResultForSuccess(Map<String, Object> data, String message) {
		return responseResult(data, null, false, message);
	}

	/**
	 * 返回数据（成功）
	 * 
	 * @param data
	 * @return
	 */
	protected Object responseResultForSuccess(Map<String, Object> data) {
		return responseResult(data, null, false, "");
	}

	/**
	 * 返回数据
	 * 
	 * @param data
	 * @param filed
	 * @param error
	 * @param message
	 * @return
	 */
	private Object responseResult(Map<String, Object> data, String filed, boolean error, String message) {
		Map<String, Object> dataMap = new HashMap<>();
		if (filed == null) {
			filed = "";
		}
		if (message == null) {
			message = "";
		}
		dataMap.put(MsgConstants.DATA, data);
		dataMap.put(MsgConstants.FIELD, filed);
		dataMap.put(MsgConstants.ERROR, error);
		dataMap.put(MsgConstants.MESSAGE, message);

		String sessionId = SecurityUtils.getSubject().getSession().getId().toString();
		TokenManager tokenManager = SpringUtil.getBean(TokenManager.class);
		tokenManager.updateSessionId(sessionId);

		return dataMap;
	}

	/**
	 * 返回成功结果和自定义成功信息
	 * 
	 * @param data 返回的数据信息
	 * @param msg  自定义的成功信息
	 * @return
	 */
	public static Map<String, Object> returnSuccessMsg(Map<String, Object> data, String msg) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("status", SUCCESS);
		map.put("msg", msg);
		map.put("data", data);

		String sessionId = SecurityUtils.getSubject().getSession().getId().toString();
		TokenManager tokenManager = SpringUtil.getBean(TokenManager.class);
		tokenManager.updateSessionId(sessionId);

		return map;
	}

	/**
	 * 返回失败结果和提示信息自己定义
	 * 
	 * @param msg 自定义的错误信息
	 * @return
	 */
	public static Map<String, Object> returnFailureMsg(String msg) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("status", FAILURE);
		map.put("msg", msg);
		map.put("data", new HashMap<String, Object>());

		String sessionId = SecurityUtils.getSubject().getSession().getId().toString();
		TokenManager tokenManager = SpringUtil.getBean(TokenManager.class);
		tokenManager.updateSessionId(sessionId);

		logger.error("++++++++++ 返回失败信息 :" + msg + "  ++++++++++");
		return map;
	}

	/**
	 * 得到PageData
	 */
	public PageData getPageData() {
		return new PageData(this.getRequest());
	}

	/**
	 * 得到request对象
	 */
	public HttpServletRequest getRequest() {
		HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes())
				.getRequest();
		return request;
	}

	/**
	 * 用于计算接口的响应时间（多线程时除外）
	 * 
	 * @author tanglin
	 */
	public static void logBefore(Logger logger, String interfaceName) {
		logger.info("");
		logger.info("++++++++++  start  ++++++++++");
		logger.info(interfaceName);
		startTime = System.currentTimeMillis();
	}

	public static void logAfter(Logger logger) {
		endTime = System.currentTimeMillis();
		logger.info("++++++++++   " + (endTime - startTime) + "毫秒   ++++++++++");
		logger.info("++++++++++   end   ++++++++++");
		logger.info("");
	}

}
